import { IBehavior } from "./Behavior";
import { EnumBehaviorReturn } from "./EnumBehaviorReturn";

/**
 * 顺序执行
 *
 * @export
 * @class SequenceNode
 * @extends {BehaviorBaseNode}
 */
export class SequenceNode {
    private chidren: Array<IBehavior> = [];

    constructor() {

        this.chidren = this.chidren;
    }

    public execute(): void {
        for (const child of this.chidren) {

            const state: EnumBehaviorReturn = child.getActionState();

            if (state === EnumBehaviorReturn.RUNNING) {
                break;
            }

            if (child.condition()) {
                child.action();
            }
        }
        
    }

    public addChild(behavior: IBehavior): void {
        this.chidren.push(behavior);
    }
}